{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d00fc083",
   "metadata": {
    "papermill": {
     "duration": 0.002622,
     "end_time": "2022-10-26T08:27:02.102770",
     "exception": false,
     "start_time": "2022-10-26T08:27:02.100148",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# util-cos"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "645dc2c4",
   "metadata": {
    "papermill": {
     "duration": 0.004143,
     "end_time": "2022-10-26T08:27:02.111011",
     "exception": false,
     "start_time": "2022-10-26T08:27:02.106868",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "This component provides COS utility functions (e.g. creating a bucket, listing contents of a bucket)\n",
    "\n",
    "Open Issues:\n",
    "- [] make sure endpoint starts with https independent of input start is empty, s3 or s3a\n",
    "- [] make sure there is a / symbol between bucket and path although not specified"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eeb2d84",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install aiobotocore botocore s3fs claimed-c3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "054a358d",
   "metadata": {
    "papermill": {
     "duration": 0.013833,
     "end_time": "2022-10-26T08:27:04.440049",
     "exception": false,
     "start_time": "2022-10-26T08:27:04.426216",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import logging\n",
    "import os\n",
    "import re\n",
    "import s3fs\n",
    "import sys\n",
    "import glob\n",
    "from c3.operator_utils import explode_connection_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcab9b87",
   "metadata": {
    "papermill": {
     "duration": 0.013675,
     "end_time": "2022-10-26T08:27:04.455836",
     "exception": false,
     "start_time": "2022-10-26T08:27:04.442161",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "\n",
    "# cos_connection in format: [cos|s3]://access_key_id:secret_access_key@endpoint/bucket/path\n",
    "cos_connection = os.environ.get('cos_connection')\n",
    "\n",
    "# local_path for uploads, downloads, sync\n",
    "local_path = os.environ.get('local_path')\n",
    "\n",
    "# recursive\n",
    "recursive = bool(os.environ.get('recursive','False'))\n",
    "\n",
    "# operation (mkdir|ls|find|get|put|rm|sync_to_cos|sync_to_local|glob)\n",
    "operation = os.environ.get('operation')\n",
    "\n",
    "# log level\n",
    "log_level = os.environ.get('log_level', 'INFO')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "307d42c7-d81b-4bf1-b823-431e7c434631",
   "metadata": {
    "papermill": {
     "duration": 0.965251,
     "end_time": "2022-10-26T08:27:05.463197",
     "exception": false,
     "start_time": "2022-10-26T08:27:04.497946",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def run(cos_connection, local_path, operation, recursive = False, log_level = logging.INFO):\n",
    "    (access_key_id, secret_access_key, endpoint, cos_path) = explode_connection_string(cos_connection)\n",
    "    s3 = s3fs.S3FileSystem(\n",
    "        anon=False,\n",
    "        key=access_key_id,\n",
    "        secret=secret_access_key,\n",
    "        client_kwargs={'endpoint_url': endpoint}\n",
    "    )\n",
    "\n",
    "    if operation == 'mkdir':\n",
    "        s3.mkdir(cos_path)\n",
    "    elif operation == 'ls':\n",
    "        print(s3.ls(cos_path))\n",
    "    elif operation == 'find':\n",
    "        print(s3.find(cos_path))\n",
    "    elif operation == 'put':\n",
    "        print(s3.put(local_path,cos_path, recursive=recursive))\n",
    "    elif operation == 'sync_to_cos':\n",
    "        for file in glob.glob(local_path, recursive=recursive):\n",
    "            logging.info(f'processing {file}')\n",
    "            if s3.exists(cos_path+file):\n",
    "                logging.info(f'exists {file}')\n",
    "                logging.debug(f's3.info {s3.info(cos_path+file)}')\n",
    "                if s3.info(cos_path+file)['size'] != os.path.getsize(file):\n",
    "                    logging.info(f'uploading {file}')\n",
    "                    s3.put(file, cos_path+file)\n",
    "                else:\n",
    "                    logging.info(f'skipping {file}')\n",
    "            else:\n",
    "                logging.info(f'uploading {file}')\n",
    "                s3.put(file, cos_path+file)\n",
    "    elif operation == 'sync_to_local':\n",
    "        for full_path in s3.glob(cos_path):\n",
    "            local_full_path = local_path+full_path\n",
    "            logging.info(f'processing {full_path}')\n",
    "            if s3.info(full_path)['type'] == 'directory':\n",
    "                logging.debug(f'skipping directory {full_path}')\n",
    "                continue\n",
    "            if os.path.exists(local_full_path):\n",
    "                logging.info(f'exists {full_path}')\n",
    "                logging.debug(f's3.info {s3.info(full_path)}')\n",
    "                if s3.info(full_path)['size'] != os.path.getsize(local_full_path):\n",
    "                    logging.info(f'downloading {full_path} to {local_full_path}')\n",
    "                    s3.get(full_path, local_full_path)\n",
    "                else:\n",
    "                    logging.info(f'skipping {full_path}')\n",
    "            else:\n",
    "                logging.info(f'downloading {full_path} to {local_full_path}')\n",
    "                s3.get(full_path, local_full_path)\n",
    "    elif operation == 'get':\n",
    "        s3.get(cos_path, local_path, recursive=recursive)\n",
    "    elif operation == 'rm':\n",
    "        s3.rm(cos_path, recursive=recursive)\n",
    "    elif operation == 'glob':\n",
    "        print(s3.glob(cos_path))\n",
    "    else:\n",
    "        logging.error(f'operation unkonwn {operation}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a2a76cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    run(cos_connection, local_path, operation, recursive, log_level)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 5.223558,
   "end_time": "2022-10-26T08:27:05.697642",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "output_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "parameters": {},
   "start_time": "2022-10-26T08:27:00.474084",
   "version": "2.3.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
